package com.fanfanwang.ffw.app.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.bean.IsRegBean;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;
import com.fanfanwang.ffw.app.asynctask.ParkIsRegAsyncTask;
import com.fanfanwang.ffw.app.asynctask.SetparkNewPasswordAsyncTask;

/** 找回密码 */
public class FindPwdAct extends com.fanfanwang.ffw.app.base.BaseActivity {

	private CommonTitleBar commonTitleBar;
	private EditText re_et_mobile;
	private Button re_bt_getcode;
	private EditText re_et_code;
	private EditText re_et_password;
	private Button re_btn_complete;
	public String mobile = "";
	public String securityCode = "";
	public String password = "";
	private Activity mAct;
	private Intent mIntent;
	public StringBuffer smsString = new StringBuffer();// 这个是判断这个手机号是合法的，可以注册，然后返回的验证码
	public static IIsRegReturnable iIsRegReturnable;
	public static ISetNewPasswordable iSetNewPasswordable;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWidget();
		initData();
		AppManager.getAppManager().addActivity(this);
		widgetClick();
	}

	public void initWidget() {
		setContentView(R.layout.activity_find_pwd);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		re_bt_getcode=(Button)findViewById(R.id.re_bt_getcode);
		re_et_mobile=(EditText)findViewById(R.id.lo_mobile);
		re_et_code=(EditText)findViewById(R.id.lo_yzm);
		re_et_password=(EditText)findViewById(R.id.lo_password);
		re_btn_complete=(Button)findViewById(R.id.lo_btn_complete);
	}


	public void widgetClick() {
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		re_bt_getcode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//				new GetRegisterCodeAsyncTask().execute("");
				//				new MyCount(60000, 1000).start();
				mobile=re_et_mobile.getText().toString();
				if(TextUtils.isEmpty(mobile)){
					ToastAlone.showToast(mAct, getString(R.string.mobile));
				}else{
					new ParkIsRegAsyncTask(mAct,mobile).execute("");
				}
			}
		});
		re_btn_complete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				securityCode = re_et_code.getText().toString();
				password = re_et_password.getText().toString();
				mobile = re_et_mobile.getText().toString();
				if(TextUtils.isEmpty(mobile)){
					ToastAlone.showToast(FindPwdAct.this, getString(R.string.register_tel_no_null));
				}else if(TextUtils.isEmpty(securityCode)){
					ToastAlone.showToast(FindPwdAct.this, getString(R.string.register_yzm_no_null));
				}else if(TextUtils.isEmpty(password)){
					ToastAlone.showToast(FindPwdAct.this, getString(R.string.register_pwd_no_null));
				}else if(!securityCode.equals(smsString.toString())){
					ToastAlone.showToast(FindPwdAct.this, getString(R.string.yzm_failed));
				}
				else{
					new SetparkNewPasswordAsyncTask(mAct, mobile, password).execute("");
				}
			}
		});
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.find_pwd));
		commonTitleBar.setReturnVisible();
		setInterface();
	}

	public class MyCount extends CountDownTimer {

		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			re_bt_getcode.setText(getString(R.string.register_reget));
			re_bt_getcode.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			re_bt_getcode.setText("(" + millisUntilFinished / 1000 + ")");
			re_bt_getcode.setClickable(false);
		}

	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

	public interface IIsRegReturnable {//验证码
		public void SetIsRegSms(IsRegBean data);
		public void setIsRegSmsFailed();
	}
	public interface ISetNewPasswordable{
		public void setNewPasswordSuccess();
	}

	public void setInterface() {
		iIsRegReturnable=new IIsRegReturnable() {

			@Override
			public void setIsRegSmsFailed() {
				// TODO Auto-generated method stub
				new MyCount(60000, 1000).start();
			}

			@Override
			public void SetIsRegSms(IsRegBean data) {
				// TODO Auto-generated method stub
				smsString.append(data.sms);
				new MyCount(60000, 1000).start();
			}
		};
		iSetNewPasswordable=new ISetNewPasswordable() {

			@Override
			public void setNewPasswordSuccess() {
				// TODO Auto-generated method stub
				SharedPreferencesUtil.getInstance(mAct).putParkingId(null);
				SharedPreferencesUtil.getInstance(mAct).putTransactor(null);
				SharedPreferencesUtil.getInstance(mAct).putPianqu(null);
				SharedPreferencesUtil.getInstance(mAct).putChargetype(null);
				SharedPreferencesUtil.getInstance(mAct).putChargeprice(null);
				AppManager.getAppManager().finishAllActivity();
				AppConstant.user_id = "";
				AppConstant.username = "";
				Intent mIntent = new Intent(mAct,
						LoginAct.class);
				startActivity(mIntent);
				finish();
			}
		};

	}
}
